<?php

namespace App\Http\Controllers\Test;

use App\Http\Classes\Common;
use App\Http\Classes\Phone;
use App\Http\Classes\Sentence;
use App\Http\Classes\User;
use App\Http\Classes\UserHard;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;

class TestController extends Controller
{
    //
    public function getPhoneCode(Request $request)
    {
        if ( $request->has('phone') ) {
            $phone = new PhoneAli($request->get('phone'));
            if ($phone->sendRegisterCode()) {
                var_dump($phone->getResponse());
            } else {
                var_dump($phone->getMessage());
            }
        }
        else
        {
            return $this->response(0, 'No phone request', null);
        }
    }

    public function test()
    {
        $number = 'a1234';
        if ( is_numeric($number) )
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    public function getSession()
    {
        return captcha_img();

    }

    public function getCaptcha()
    {
        return captcha_img();
    }

    public function resetPage()
    {
        return view('tests.index');
    }

    public function redis()
    {
        Redis::set('test', 123456);
        echo Redis::get('test');
    }

    public function validateCaptcha(Request $request)
    {
        var_dump(captcha_check($request->get('captcha')));
//        dd($this->validate($request, [
//            'captcha' => 'required|captcha'
//        ]));
    }

    public function server()
    {
        dd($_SERVER);
    }

    public function word()
    {
        $path = public_path() . '\\words.txt';
        $file = fopen($path, 'r');
        var_dump( fgetcsv($file,5000000,'['));
        $string = file_get_contents($path);
        $string = mb_convert_encoding($string, 'UTF-8', 'GBK');
        echo $string;
    }

    public function sentence()
    {
//        $myfile = fopen(public_path() ."\Lesson1\\2-4.txt", "r") or die("Unable to open file!");
//        //fread($myfile,filesize(public_path() ."\Lesson1\\1.txt"));
//        $string = mb_convert_encoding(fgets($myfile),'UTF-8', 'GBK');
//        echo $string;
//        fclose($myfile);
        //dd($myfile);
        $dir = public_path() ."\Lesson1/";
// Open a known directory, and proceed to read its contents
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    $type = filetype( $dir . $file );

                    if ( $type == 'file')
                    {

                        echo "filename: $file : filetype: " . filetype($dir . $file) . '<br>';
                    }

                }
                closedir($dh);
            }
        }
    }

    public function not()
    {
        return view('errors.404');
    }

    public function def()
    {
        return view('errors.403');
    }

    public function vip()
    {
        $user = new User(1);
        dd($user->isVip());
    }

    public function sentences()
    {
        $sentence = new Sentence();

        $sentences = $sentence->getSentencesByLessonId(1);

        dd($sentences);
    }

    public function password(Request $request)
    {
        var_dump(Common::isPassword($request->get('pwd')));
    }

    public function left()
    {
        $words = new UserHard();

        var_dump($words->getUserHardWords($_COOKIE['USER_ID']));
    }

    public function delhard()
    {
        $words = new UserHard(1);

        var_dump($words->delWord($_COOKIE['USER_ID']));
    }

    public function host()
    {
        dd(HOST);
    }

    public function getUserHardSentences()
    {
        $hard = new UserHard();

        $hards = $hard->getUserHardSentences($_COOKIE['USER_ID']);

        dd($hards);
    }

    public function getUserHardWords()
    {
        $hard = new UserHard();

        $hards = $hard->getUserHardWords($_COOKIE['USER_ID']);

        dd($hards);
    }

    public function weiboView()
    {
        return view('front.weiboCallback');
    }

    public function weibo()
    {
        echo WB_AKEY;
    }

    public function excel()
    {
        set_time_limit(360);
        $uploadfile = public_path() . '\\orignal_data\\9-1.xlsx';
        require_once dirname(__FILE__) . '/../../Classes/PHPExcel/IOFactory.php';
        $reader = \PHPExcel_IOFactory::createReader('Excel2007');
        $PHPExcel = $reader->load($uploadfile);
        $objWorksheet = $PHPExcel->getAllSheets();
        $objWorksheet = $objWorksheet[0];
        $highestRow = $objWorksheet->getHighestRow(); // 取得总行数
        $highestColumn = $objWorksheet->getHighestColumn(); // 取得总列数
        $arr = array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G', 8 => 'H', 9 => 'I', 10 => 'J', 11 => 'K', 12 => 'L', 13 => 'M', 14 => 'N', 15 => 'O', 16 => 'P', 17 => 'Q', 18 => 'R', 19 => 'S', 20 => 'T', 21 => 'U', 22 => 'V', 23 => 'W', 24 => 'X', 25 => 'Y', 26 => 'Z');
//echo $highestRow.$highestColumn;
// 一次读取一列
        $res = array();
        for ($row = 2; $row <= $highestRow; $row++) {
            for ($column = 0; $arr[$column] != 'G'; $column++) {
                $val = $objWorksheet->getCellByColumnAndRow($column, $row)->getValue();
                $res[$row - 2][$column] = $val;
            }
        }

        dd($res);
    }

    public function aliyunEMS()
    {
//        $phone = new PhoneAli(15928731747);
//        $result = $phone->aliyunSMS();
//        return $result;
    }

}
